<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
    class="page-nav-bar"
    title="个人信息"
    left-arrow @click-left="$router.back()" />
    <!-- /导航栏 -->
    <input type="file" @change="onChange" hidden ref="iptFile">
    <van-cell title="头像" @click="$refs.iptFile.click()" is-link>
      <van-image
        class="avatar"
        fit="cover"
        round
        center
        :src="userinfo.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="editPopupShow=true" :value="userinfo.name" is-link />
    <van-cell title="性别" @click="SexPopupShow=true" :value="userinfo.gender?'女':'男'" is-link />
    <van-cell title="生日" @click="briPopupShow=true" :value="userinfo.birthday" is-link />
    <van-popup v-model="editPopupShow" position="bottom" :style="{ height: '100%' }" >
      <user-edit v-if="editPopupShow" @close="editPopupShow=false" v-model="userinfo.name"/>
    </van-popup>
    <van-popup v-model="SexPopupShow" position="bottom" >
      <user-sex-edit v-if="SexPopupShow" v-model="userinfo.gender" @close="SexPopupShow=false" />
    </van-popup>
    <van-popup v-model="briPopupShow" position="bottom" >
      <user-bri-edit v-if="briPopupShow" v-model="userinfo.birthday" @close="briPopupShow=false" />
    </van-popup>
    <van-popup v-model="imgPopupShow" style="height:100%" position="bottom" >
      <user-edit-avatar v-if="imgPopupShow" @update-photo="userinfo.photo=$event" @close="imgPopupShow=false" :imgUrl="imgUrl" />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import { Toast } from 'vant'
import UserEdit from './components/UserEdit.vue'
import UserSexEdit from './components/UserSexEdit.vue'
import UserBriEdit from './components/UserBriEdit.vue'
import UserEditAvatar from './components/UserEditAvatar.vue'
export default {
  components: { UserEdit, UserSexEdit, UserBriEdit, UserEditAvatar },
  data () {
    return {
      userinfo: {},
      editPopupShow: false,
      SexPopupShow: false,
      briPopupShow: false,
      imgPopupShow: false,
      imgUrl: ''
    }
  },

  created () {
    this.loadUserProfile()
  },

  methods: {
    async loadUserProfile () {
      try {
        const res = await getUserProfile()
        this.userinfo = res
      } catch (err) {
        Toast.fail()
      }
    },
    onChange (e) {
      if (!e.target.value) return false
      this.imgPopupShow = true
      this.imgUrl = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
